const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,
    mediaPath: '',
    imageList: [],
    showIndicator: true,
    pageNum: 1,
    total: 0,
    current: 1,
    baseUrl: 'http://127.0.0.1:9093/media/',
    scrollTop: null,
    showMask: false,
    isPageLoaded: false,
    pageHeight:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const collection = wx.getStorageSync('collection')
    this.setData({
      id: collection.mediaId,
      pageNum: collection.index,
      scrollTop: 0 // 重新加载时重置滚动位置
    })
    app.$http.get('/app/media/list', { pageNum: 1, pageSize: 1, collectionId: collection.id }).then(res => {
      this.setData({ total: res.data.total })
    })
    this.initImageList();
    setTimeout(() => {
      this.setData({ isPageLoaded: true }); // 页面加载完成后设置标志位为 true
    }, 500); // 适当延迟，确保页面元素加载完成
  },
  initImageList: function () {
    app.$http.get('/app/media/list', { pageNum: this.data.pageNum + 1, pageSize: 1, collectionId: wx.getStorageSync('collection').id }).then(res => {
      this.setData({ mediaPath: res.data.records[0].mediaPath })
      const path = 'comic' + this.data.mediaPath
      app.$http.get('/file/list', { mediaPath: path }).then(res => {
        this.setData({ imageList: res.data.map(item => ({ url: this.data.baseUrl + item })) })
      })
    })
  },
  onPageScroll(e) {
    if (!this.data.isPageLoaded) return; // 页面未加载完成，不执行滚动逻辑
    const y = e.detail.scrollTop;
    const pageHeight = Math.round(e.detail.scrollHeight / this.data.imageList.length)
    const current = Math.ceil((y + pageHeight) / pageHeight); // 计算当前页索引
    if (current !== this.data.current && current > 0) {
      this.setData({ current });
    }
  },
  //滑动到底部
  reachBottom() {
    if (this.data.pageNum == this.data.total - 1) {
      wx.showToast({
        title: '已经到头咯~',
        icon: 'none',
        duration: 500
      })
      return false;
    }
    this.setData({ showMask: true })
    app.$http.get('/app/media/list', { pageNum: this.data.pageNum + 2, pageSize: 1, collectionId: wx.getStorageSync('collection').id }).then(res => {
      this.setData({mediaPath: res.data.records[0].mediaPath})
      const path = 'comic' + this.data.mediaPath
      app.$http.get('/file/list', { mediaPath: path}).then(res => {
        this.setData({
          scrollTop: 50
        });
        setTimeout(() => {
          this.setData({
            pageNum: this.data.pageNum + 1,
            imageList: res.data.map(item => ({ url: this.data.baseUrl + item })),
            current: 1,
          })
          this.setData({
            showMask: false,
            scrollTop: 50
          });
        }, 2000);
      })
    })
  },
  //滑动到顶部
  reachTop() {
    if (this.data.pageNum == 0) {
      wx.showToast({
        title: '已经到头咯~',
        icon: 'none',
        duration: 500
      })
      return false;
    }
    this.setData({ showMask: true })
    app.$http.get('/app/media/list', { pageNum: this.data.pageNum, pageSize: 1, collectionId: wx.getStorageSync('collection').id }).then(res => {
      this.setData({
        mediaPath: res.data.records[0].mediaPath
      })
      const path = 'comic' + this.data.mediaPath
      app.$http.get('/file/list', { mediaPath: path }).then(res => {
        setTimeout(() => {
          this.setData({
            pageNum: this.data.pageNum - 1,
            imageList: res.data.map(item => ({ url: this.data.baseUrl + item })),
            current: 1,
            showMask: false
          })
        }, 2000);
      })
    })
  },
  onUnload(){
    let collection = wx.getStorageSync('collection')
    app.$http.get('/app/collection/'+collection.id).then(res=>{
      collection = res.data
      collection.views = collection.views + 1
      app.$http.put('/app/collection/update', collection)
    })
  }
})    